/*******************************************************************************
New Unit Technologies, a division of DBI, LLC.

Created for CS450 at West Virginia University, Fall 2003

Developers:	Matthew Bradley
		Andrew Lowery
		Richard Paletta
		J. Ryan Sigler

File name: pcb.c
*******************************************************************************/

#include "pcb.h"
#include <ctype.h>
#include <string.h>
#include "mpx_supt.h"
#include "int_supt.h"

// initialize pcb and queue

extern PCB *running;
extern queue *READY_QUEUE;
extern queue *BLOCKED_QUEUE;
extern queue *SUS_BLOCKED_QUEUE;
extern queue *SUS_READY_QUEUE;

/*******************************************************************************
Procedure name: newpcb()
Purpose: initializes memory to create a new pcb
Authors Names: Rich Paletta, Andrew Lowery
Parameters: none
Returns: pcbptr
Globals: PCB
Calls: sys_alloc_mem
Change log: 10/4/03 - Rich writes inital code
	    10/5/03 - Drew adds comments
	    10/7/03 - Ryan changes function prototype
*******************************************************************************/

PCB  *newPCB(void)
{
	size_t size=sizeof(PCB);                     	// find needed size of pcb
	void *pcbptr;
	pcbptr=sys_alloc_mem(size);                  	// allocate needed memory

	if(pcbptr==NULL)                             	// check to see if pcb can be created
	{
		printf("ERROR CREATING NEW PCB");
	}

	return pcbptr;
}


/**************************************************************
Procedure name: setuppcb()
Purpose: assigns the attributes to the pcb
Authors Names: Rich Paletta, Andrew Lowery
Parameters: PCB *newPCB, char newname[], int newpclass,
				int newpriority
Returns: none
Globals: none
Calls: sys_alloc_mem
Change log: 9/30/03 - Rich writes inital code
	    10/2/03 - Ryan adds next and prev initializations	
	    10/5/03 - Drew adds comments
**************************************************************/

void setupPCB(PCB *newPCB, char newname[], int newpclass, int newpriority, int memory, int offset)
{
	strcpy(newPCB->name,newname);           	// assign attributes to pcb
	newPCB->pclass=newpclass;
	newPCB->priority=newpriority;
	newPCB->state = READY;
	newPCB->stack = (unsigned char*) sys_alloc_mem(1000);
	newPCB->mem_size = memory;
	newPCB->load = ((unsigned char*) sys_alloc_mem(memory));
	newPCB->exe = (*newPCB).load + offset;
	newPCB->top = (*newPCB).stack+1000-sizeof(context);


	//initialize next and prev

	newPCB -> next = NULL;
	newPCB -> prev = NULL;
}

void killPCB(PCB *pcb_ptr){
     sys_free_mem((*pcb_ptr).stack);
     sys_free_mem((*pcb_ptr).load);
     sys_free_mem((*pcb_ptr).exe);
     sys_free_mem((*pcb_ptr).top);
     sys_free_mem(pcb_ptr);
}


